package me.ilich.piet.interpreter;

import java.security.InvalidParameterException;
import java.util.LinkedHashMap;
import java.util.Map;

public class Program {

	private Map<Coordinate,Codel> mCodels = new LinkedHashMap<Coordinate, Codel>();
	private final int mWidth;
	private final int mHeight;
	
	public Program(int width, int height){
		mWidth = width;
		mHeight = height;
	}
	
	public Codel getCodel(Coordinate coordinate){
		if(coordinate==null) throw new NullPointerException("coordinate");
		if(coordinate.getX()>=mWidth) throw new InvalidParameterException("x");
		if(coordinate.getY()>=mHeight) throw new InvalidParameterException("y");
		Codel result;
		if(mCodels.containsKey(coordinate)){
			result = mCodels.get(coordinate);
		}else{
			result = Codel.instanceWhite();
		}
		return result;
	}
	
	public Codel getCodel(int x, int y){
		Codel result = getCodel(Coordinate.instance(x, y));
		return result;
	}
	
	public void setCodel(Coordinate coordinate, Codel codel){
		if(coordinate==null) throw new NullPointerException("coordinate");
		if(codel==null) throw new NullPointerException("codel");
		if(coordinate.getX()>=mWidth) throw new InvalidParameterException("x");
		if(coordinate.getY()>=mHeight) throw new InvalidParameterException("y");
		mCodels.put(coordinate, codel);
	}
	
	public void setCodel(int x, int y, Codel codel){
		setCodel(Coordinate.instance(x, y), codel);
	}

	public void debug() {
		for(Coordinate coord : mCodels.keySet()){
			Codel codel = mCodels.get(coord);
			System.out.println(coord + " " + codel);
		}
	}
	
	
	
}
